﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Entity;
using _测试EF.Model;
using System.Data.Entity.Infrastructure;

namespace _测试
{
    public partial class Form1 : Form
    {
        public List<Student> StudentList { get; set; }
        public Form1()
        {
            InitializeComponent();
            BtnEventInit();
        }


        public void BtnEventInit()
        {
            btnCreateStudent.Click += new EventHandler(btnCreateStudent_Click);
            btnCreateFraction.Click += new EventHandler(btnCreateFraction_Click);
        }

        public void btnCreateStudent_Click(object sender, EventArgs e)
        {
            List<Student> stuList = new List<Student>();
            try
            {
                string HanziData = CreateHanzi(1800);
                List<string> classNames = GetClassName();

                Random rd = new Random();
                for (int i = 0; i < classNames.Count; i++)
                {
                    for (int n = 1; n < 37; n++)
                    {
                        Student student = new Student();
                        student.ClassNum = classNames[i];
                        student.Class = student.ClassNum.Substring(0, 2);
                        student.Name = HanziData[rd.Next(1800)].ToString() + HanziData[rd.Next(1800)].ToString() + HanziData[rd.Next(1800)].ToString();
                        student.StuNum = "2017" + rd.Next(100000, 1000000);
                        student.Sex = rd.Next(0, 2) > 0 ? "男" : "女";
                        using (var db = new BaseContext())
                        {
                            int count = db.Student.Where(o => o.StuNum == student.StuNum).Count();
                            if (count > 0)
                            {
                                i--;
                            }
                            else
                            {
                                db.Student.Add(student);
                                db.SaveChanges();
                                stuList.Add(student);
                            }
                        }
                    }
                }

                MessageBox.Show("学生生成完成");
                StudentList = stuList;
            }

            catch (DbUpdateException ex)
            {
                string name = ex.GetType().Name;
                MessageBox.Show("失败!" + ex.Message.ToString());
                throw;
            }

        }

        public void btnCreateFraction_Click(object sender, EventArgs e)
        {
            List<DateTime> times = ExamTimes();
            try
            {
                Random rd = new Random();
                for (int i = 0; i < StudentList.Count; i++)
                {
                    for (int z = 0; z < times.Count; z++)
                    {
                        for (int j = 1; j < 8; j++)
                        {
                            StuFraction fraction = new StuFraction();
                            fraction.CourseName = NumberToCourse(j);
                            fraction.CreateBy = "System";
                            fraction.CreateTime = DateTime.Now;
                            fraction.ExamCode = StudentList[i].StuNum;
                            fraction.StuId = StudentList[i].Id;
                            fraction.Score = rd.Next(1, 101);
                            fraction.ExamTime = times[z];
                            using (var db = new BaseContext())
                            {
                                db.Set<StuFraction>().Add(fraction);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                MessageBox.Show("生成成绩成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                throw;
            }

        }

        public string CreateHanzi(int count)
        {
            Random rd = new Random();
            var bs = new byte[count * 2];
            for (int i = 0; i < count; i++)
            {
                var c = GetGB2312Char(rd);
                bs[i * 2] = (byte)(c.X + 0xa0);
                bs[i * 2 + 1] = (byte)(c.Y + 0xa0);

            }
            //List<string> Hanzi = new List<string>();
            //Hanzi.Add(Encoding.GetEncoding("GB2312").GetString(bs));
            return Encoding.GetEncoding("GB2312").GetString(bs);
        }

        public List<string> GetClassName()
        {
            List<string> ClassList = new List<string>();
            for (int i = 1; i < 4; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    ClassList.Add("高" + NumberToChainese(j) + i + "班");
                }
            }
            return ClassList;
        }

        /// <summary>
        /// 考试时间
        /// </summary>
        /// <returns></returns>
        public List<DateTime> ExamTimes()
        {
            List<DateTime> times = new List<DateTime>();
            for (int i = 0; i < 7; i++)
            {
                times.Add(DateTime.Now.AddDays(-30 * i));
            }
            return times;
        }

        Point GetGB2312Char(Random random)
        {
            // 国标一级字(共3755个): 区:16-55, 位:01-94, 55区最后5位为空位
            var 区 = random.Next(40) + 16;
            var 位 = random.Next(区 == 55 ? 89 : 94) + 1;
            return new Point(区, 位);
        }

        public string NumberToChainese(int num)
        {
            string Level = string.Empty;
            switch (num)
            {
                case 1: Level = "一"; break;
                case 2: Level = "二"; break;
                case 3: Level = "三"; break;
            }
            return Level;
        }

        public string NumberToCourse(int num)
        {
            string Course = string.Empty;
            switch (num)
            {
                case 1:
                    Course = "语文"; break;
                case 2: Course = "数学"; break;
                case 3: Course = "英语"; break;
                case 4: Course = "历史"; break;
                case 5: Course = "地理"; break;
                case 6: Course = "物理"; break;
                case 7: Course = "化学"; break;
            }
            return Course;
        }

        private void btnCS_Click(object sender, EventArgs e)
        {
            long n = DateTime.Now.Ticks;

            //using (var db = new BaseContext())
            //{
            //    var list = (from a in db.Product
            //                select new
            //                {
            //                    Name = a.Name,
            //                    RenQi = a.RenQi,
            //                    Orders = a.RenQi * 1.000 / DbFunctions.DiffMilliseconds(a.InputTime,DateTime.Now)
            //                }).ToList();
            //}
        }

        public void btnDD_Click()
        {
            decimal a = 1.56M;
            int b = 1;
            if (a > b)
            {

            }
        }
    }
}
